<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

/**
 * 群发消息表
 */
class CreateWxMass extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('wx_mass', function (Blueprint $table) {
            $table->increments('mass_id')->comment('群发消息ID');
            $table->string('appid', 64)->default('')->comment('微信应用ID');
            $table->unsignedInteger('msg_id')->default(0)->comment('消息发送任务的ID');
            $table->string('title', 150)->default('')->comment('消息标题');
            $table->mediumText('description')->nullable()->comment('消息描述');
            $table->string('msg_type', 50)->default('')->comment('消息类型');
            $table->text('filter_rule')->nullable()->comment('过滤规则');
            $table->unsignedTinyInteger('is_open')->default(0)->comment('是否开启');
            $table->timestamp('push_time')->nullable()->comment('群发推送时间,为空时立即发送');
            $table->mediumText('content')->nullable()->comment('群发文本内容');
            $table->unsignedInteger('material_id')->default(0)->comment('素材表主键');
            $table->string('card_id', 50)->default('')->comment('卡券');
            $table->unsignedTinyInteger('send_ignore_reprint')->default(1)->comment('图文消息被判定为转载时，是否继续群发');
            $table->unsignedTinyInteger('is_send')->default(0)->comment('是否发送');
            $table->unsignedInteger('errcode')->default(0)->comment('错误码');
            $table->string('errmsg', 200)->default('')->comment('错误信息');
            $table->string('msg_data_id', 30)->default('')->comment('消息的数据ID,该字段只有在群发图文消息时才会出现');
            $table->softDeletes();
            $table->timestamps();
            $table->index(['appid','msg_id','deleted_at'], 'idx_1');
            $table->index(['appid','is_open','push_time','deleted_at'], 'idx_2');
        });
        Schema::create('wx_mass_results', function (Blueprint $table) {
            $table->increments('id')->comment('自增ID');
            $table->string('appid', 64)->default('')->comment('微信应用ID');
            $table->unsignedInteger('msg_id')->default(0)->comment('消息发送任务的ID');
            $table->string('status', 50)->default('')->comment('状态');
            $table->unsignedInteger('total_count')->default(0)->comment('目标推送的粉丝数');
            $table->unsignedInteger('filter_count')->default(0)->comment('过滤后的粉丝数');
            $table->unsignedInteger('sent_count')->default(0)->comment('发送成功的粉丝数');
            $table->unsignedInteger('error_count')->default(0)->comment('发送失败的粉丝数');
            $table->unsignedInteger('check_state')->default(0)->comment('整体校验结果');
            $table->softDeletes();
            $table->timestamps();
            $table->index(['appid','msg_id','deleted_at'], 'idx_1');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('wx_mass');
        Schema::dropIfExists('wx_mass_results');
    }
}
